package modelo.persistencia.JDBC;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import modelo.entidades.Vehiculo;
import modelo.entidades.VehiculoImp;
import modelo.persistencia.VehiculoDAO;

/**
 *
 * @author jjcamacho
 */
public class VehiculoDAOJDBC implements VehiculoDAO {

    @Override
    public Vehiculo read(String pk) throws SQLException, Exception {
        Vehiculo vehiculo = null;
        String sql = "select * from vehiculos where matricula = ?";
        try {
            PreparedStatement stmt = Persistencia.createConnection().prepareStatement(sql);
            stmt.setString(1, pk);
            ResultSet res = stmt.executeQuery();
            if (res.next()) {
                String matricula = res.getString("matricula");
                String usuario = res.getString("usuario");
                String grupo = res.getString("grupo");
                vehiculo = new VehiculoImp(matricula);
                vehiculo.setUsuario(usuario);
                vehiculo.setGrupo(grupo);
            }
        } finally {
            Persistencia.closeConnection();
        }
        return vehiculo;
    }

    @Override
    public Vehiculo read(Vehiculo entidad) throws SQLException, Exception {
        throw new UnsupportedOperationException("No implementado");
    }

    @Override
    public void create(Vehiculo vehiculo) throws SQLException, Exception {
        String sql = "insert into vehiculos(matricula, usuario, grupo) values (?,?,?)";
        try {
            PreparedStatement stm = Persistencia.createConnection().prepareStatement(sql);
            stm.setString(1, vehiculo.getMatricula());
            stm.setString(2, vehiculo.getUsuario());
            stm.setString(3, vehiculo.getGrupo());
            stm.execute();
        } finally {
            Persistencia.closeConnection();
        }
    }

    @Override
    public void update(Vehiculo vehiculo) throws SQLException, Exception {
        String sql = "update vehiculos set matricula=?, usuario=?, grupo=? where matricula like ?";
        try {
            PreparedStatement stm = Persistencia.createConnection().prepareStatement(sql);
            stm.setString(1, vehiculo.getMatricula());
            stm.setString(2, vehiculo.getUsuario());
            stm.setString(3, vehiculo.getGrupo());
            stm.setString(4, vehiculo.getMatricula());
            stm.executeUpdate();
        } finally {
            Persistencia.closeConnection();
        }
    }

    @Override
    public void delete(Vehiculo vehiculo) throws SQLException, Exception {
        String sql = "delete from vehiculos where matricula = ?";
        try {
            PreparedStatement stm = Persistencia.createConnection().prepareStatement(sql);
            stm.setString(1, vehiculo.getMatricula());
            stm.execute();
        } finally {
            Persistencia.closeConnection();
        }
    }

    @Override
    public List<Vehiculo> list() throws SQLException, Exception {
        List<Vehiculo> vehiculos = new ArrayList<Vehiculo>();
        try {


            Statement stmt = Persistencia.createConnection().createStatement();
            ResultSet res = stmt.executeQuery("select * from vehiculos");
            while (res.next()) {
                String matricula = res.getString("matricula");
                String usuario = res.getString("usuario");
                String grupo = res.getString("grupo");
                Vehiculo vehiculo = new VehiculoImp(matricula);
                vehiculo.setUsuario(usuario);
                vehiculo.setGrupo(grupo);
                vehiculos.add(vehiculo);
            }
        } finally {
            Persistencia.closeConnection();
        }
        return vehiculos;
    }

}
